//
//  GTTabBarController.m
//  GiftTalk
//
//  Created by Wan on 16/3/22.
//  Copyright © 2016年 Wan. All rights reserved.
//

#import "GTTabBarController.h"
#import "GTCategoryMainController.h"
#import "GTGiftViewController.h"
#import "GTHotViewController.h"
#import "GTMeViewController.h"
#import "GTNavigationController.h"
#import "GTGiftItemViewController.h"

@class GTCategoryViewController;
@implementation GTTabBarController

/**
 *  统一设置 tabBarItem 的字体大小 & 颜色
 */
+ (void)initialize
{
    /*****************************************************************************/
    /* 代码设置tabBar 图片以图片原始的方式渲染,也可找到对应的图像资源,选中进行界面化设置
     UIImage *image = [UIImage imageNamed:@"tabbar_home_selected"];
     image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
     giftVc.tabBarItem.image = image;
     */
    
    /*****************************************************************************/
   
    
    // 通过 appearance 统一设置 UITabBarItem 的文字属性
    // 只要后面带有 UI_APPEARANCE_SELECTOR 标识的方法,都可以用 appearance 对象来统一设置
    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedIn:self, nil];
    
    // 代码设置 tabbar 字体及颜色
    NSMutableDictionary *normalFontAttributes = [NSMutableDictionary dictionary];
    normalFontAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:10.0];
    normalFontAttributes[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    [tabBarItem setTitleTextAttributes:normalFontAttributes forState:UIControlStateNormal];
    
    NSMutableDictionary *selectedFontAttributes = [NSMutableDictionary dictionary];
    selectedFontAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:10.0];
    selectedFontAttributes[NSForegroundColorAttributeName] = [UIColor redColor];
    [tabBarItem setTitleTextAttributes:selectedFontAttributes forState:UIControlStateSelected];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 添加子控制器
    GTGiftViewController *giftVc = [[GTGiftViewController alloc] init];
    [self addOneChildViewController:giftVc title:@"礼物说" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    GTGiftItemViewController *hotVc = [[GTGiftItemViewController alloc] init];
    [self addOneChildViewController:hotVc title:@"热门" image:@"tabbar_gift" selectedImage:@"tabbar_gift_selected"];
    
//    [self addOneChildVcWithStoryboardName:@"GTCategory" title:@"分类" image:@"tabbar_category" selectedImage:@"tabbar_category_selected"];
    GTCategoryMainController *categoryVc = [[GTCategoryMainController alloc] init];
    [self addOneChildViewController:categoryVc title:@"分类" image:@"tabbar_category" selectedImage:@"tabbar_category_selected"];

    GTMeViewController *meVc = [[GTMeViewController alloc] init];
    [self addOneChildViewController:meVc title:@"我" image:@"tabbar_me" selectedImage:@"tabbar_me_selected"];

}

/**
 *  添加子控件
 *
 *  @param childController 子控制器
 *  @param title           tabBar显示名称
 *  @param imageName       tabBar默认图片
 *  @param selectedImage   tabBar选中图片
 */
- (void)addOneChildViewController:(UIViewController *)childVc title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImage
{
    childVc.tabBarItem.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
//    [self addChildViewController:childVc];
    
    // 包装一个导航控制器,添加导航控制器为 TabBarController 的子控制器
    GTNavigationController *nav = [[GTNavigationController alloc] initWithRootViewController:childVc];
    
    [self addChildViewController:nav];
}

- (void)addOneChildVcWithStoryboardName:(NSString *)name title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:name bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:name];
    
    vc.navigationItem.title = title;
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];;
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    
    GTNavigationController *nav = [[GTNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

@end
